package com.pai4j.common.util.community;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.community.dto.ai.VideoMindMapSummaryDTO;

import java.util.List;

public class MindMapToMarkdownConverter {

    /**
     * Converts a list of VideoMindMapSummaryDTO to a Markdown string
     *
     * @param nodes List of root nodes of the mind map
     * @return Markdown formatted string representing the mind map
     */
    public String convertMD(List<VideoMindMapSummaryDTO> nodes) {
        StringBuilder markdown = new StringBuilder();
        for (VideoMindMapSummaryDTO node : nodes) {
            convertNodeToMarkdown(node, markdown, 0);
        }
        return markdown.toString();
    }

    /**
     * Recursively converts a single node and its children to Markdown
     *
     * @param node      Current node to convert
     * @param markdown  StringBuilder to accumulate the Markdown
     * @param level     Current depth level (for indentation)
     */
    private void convertNodeToMarkdown(VideoMindMapSummaryDTO node, StringBuilder markdown, int level) {
        // Add indentation based on level
        for (int i = 0; i < level; i++) {
            markdown.append("    ");
        }

        // Add the node title with appropriate Markdown heading
        if (level == 0) {
            markdown.append("# ").append(node.getTitle()).append("\n");
        } else if (level == 1) {
            markdown.append("## ").append(node.getTitle()).append("\n");
        } else if (level == 2) {
            markdown.append("### ").append(node.getTitle()).append("\n");
        } else {
            // For deeper levels, use unordered list items
            markdown.append("- ").append(node.getTitle()).append("\n");
        }

        // Process child nodes recursively
        if (node.getTopic() != null && !node.getTopic().isEmpty()) {
            for (VideoMindMapSummaryDTO child : node.getTopic()) {
                convertNodeToMarkdown(child, markdown, level + 1);
            }
        }
    }

    public static void main(String[] args) {

        String json = "[{\"Title\":\"Net Service & Next Server Connection Handling\",\"Topic\":[{\"Title\":\"1. Net Service & Gateway\",\"Topic\":[{\"Title\":\"已完成测试，不深入讨论\",\"Topic\":[]},{\"Title\":\"直接关注Next Server的处理流程\",\"Topic\":[]}]},{\"Title\":\"2. Next Server Handle & Connection\",\"Topic\":[{\"Title\":\"连接建立成功处理\",\"Topic\":[{\"Title\":\"设置断点，刷新检查\",\"Topic\":[]},{\"Title\":\"切换至用户对话长链接\",\"Topic\":[]},{\"Title\":\"Net集群网关连接确认成功\",\"Topic\":[]}]}]},{\"Title\":\"3. 用户信息 & 长链接维护\",\"Topic\":[{\"Title\":\"维护用户连接信息\",\"Topic\":[{\"Title\":\"避免使用“掰场景”设计\",\"Topic\":[]},{\"Title\":\"仅支持单一场景设计\",\"Topic\":[]},{\"Title\":\"掰场景解释\",\"Topic\":[{\"Title\":\"基于多场景的Service实现\",\"Topic\":[]},{\"Title\":\"如PC、APP、移动端、网页版等\",\"Topic\":[]},{\"Title\":\"复杂性导致后续取值麻烦\",\"Topic\":[]}]}]}]},{\"Title\":\"4. 设计优化\",\"Topic\":[{\"Title\":\"简化设计\",\"Topic\":[{\"Title\":\"不再考虑多端支持\",\"Topic\":[]},{\"Title\":\"集中于单一场景实现\",\"Topic\":[]},{\"Title\":\"避免长链接多端管理复杂度\",\"Topic\":[]}]}]}]}]";

        List<VideoMindMapSummaryDTO> nodes = JsonUtil.fromJson(json, new TypeReference<List<VideoMindMapSummaryDTO>>() {});
        MindMapToMarkdownConverter converter = new MindMapToMarkdownConverter();
        String markdown = converter.convertMD(nodes);
        System.out.println("markdown: " + markdown);
    }
}